package com.wuzao.aop.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzao.aop.annotation.UserAccess;
import com.wuzao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;


/**
 * Description:
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/normal")
//    @UserAccess(desc = "second")
    public Object first(@RequestBody JSONObject object) {

        userService.test("1", "2");
        log.info("/normal 方法执行中...");
        return "这是 /normal 方法返回值";
    }

    @PostMapping("/doError")
    public Object error(@RequestBody JSONObject object) {
        return 1 / 0;
    }

	@PostMapping("/LINE_SEPARATOR")
    @UserAccess(desc = "second")
    public Object second() {
        return "second controller";
    }

//
//    private final WebClient webClient;
//
//    public UserController(WebClient.Builder webClientBuilder) {
//        this.webClient = webClientBuilder.baseUrl("http://127.0.0.1:9966").build();
//    }
//
//    @PostMapping("/tts")
//    public Mono<ResponseEntity<Map<String, Object>>> handleRequest(@RequestBody Map<String, Object> requestData) {
//        // 打印接收到的数据
//        System.out.println("Received data: " + requestData);
//
//        // 检查请求头中是否存在自定义标志，防止无限递归
//        if (!"true".equals(requestData.get("X-Forwarded"))) {
//            // 作为客户端将相同的请求发送到指定地址
//            return webClient.post()
//                    .uri("/tts")
//                    .header(HttpHeaders.CONTENT_TYPE, "application/json")
//                    .header("X-Forwarded", "true")
//                    .bodyValue(requestData)
//                    .retrieve()
//                    .bodyToMono(Map.class)
//                    .map(response -> {
//                        // 打印客户端请求的响应
//                        System.out.println("Forwarded request response: " + response);
//                        return ResponseEntity.ok(response);
//                    });
//        } else {
//            // 直接返回接收到的数据，避免无限递归
//            Map<String, Object> response = new HashMap<>();
//            response.put("status", "success");
//            response.put("message", "Data received and not forwarded");
//            response.put("data", requestData);
//            return Mono.just(ResponseEntity.ok(response));
//        }
//    }
}


